home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / TriFan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  7.7 KB  |  218 lines

  1. /*
  2. //   (C) COPYRIGHT International Business Machines Corp. 1993
  3. //   All Rights Reserved
  4. //   Licensed Materials - Property of IBM
  5. //   US Government Users Restricted Rights - Use, duplication or
  6. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. //
  8.  
  9. //
  10. // Permission to use, copy, modify, and distribute this software and its
  11. // documentation for any purpose and without fee is hereby granted, provided
  12. // that the above copyright notice appear in all copies and that both that
  13. // copyright notice and this permission notice appear in supporting
  14. // documentation, and that the name of I.B.M. not be used in advertising
  15. // or publicity pertaining to distribution of the software without specific,
  16. // written prior permission. I.B.M. makes no representations about the
  17. // suitability of this software for any purpose.  It is provided "as is"
  18. // without express or implied warranty.
  19. //
  20. // I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
  22. // BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  24. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  25. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. // Author:  John Spitzer, IBM AWS Graphics Systems (Austin)
  28. //
  29. */
  30.  
  31. #include <math.h>
  32. #include "TriFan.h"
  33.  
  34. #define sq(val) ((val)*(val))
  35.  
  36. #undef offset
  37. #define offset(v) offsetof(TriangleFan,v)
  38.  
  39. static InfoItem TriangleFanInfo[] = {
  40. #define INC_REASON INFO_ITEM_ARRAY
  41. #include "TriFan.h"
  42. #undef INC_REASON
  43. };
  44. #include <malloc.h>
  45.  
  46. TriangleFanPtr new_TriangleFan()
  47. {
  48.     TriangleFanPtr this = (TriangleFanPtr)malloc(sizeof(TriangleFan));
  49.     CheckMalloc(this);
  50.     new_Polygonal((PolygonalPtr)this);
  51.     SetDefaults((TestPtr)this, TriangleFanInfo);
  52.     this->testType = TriangleFanTest;
  53.     this->primitiveType = GL_TRIANGLE_FAN;
  54.     this->vertsPerFacet = 1;
  55.     this->usecPixelPrint = " microseconds per pixel with Triangle Fan";
  56.     this->ratePixelPrint = " pixels per second with Triangle Fan";
  57.     this->usecPrint = " microseconds per Triangle in a Triangle Fan";
  58.     this->ratePrint = " Triangles per second in a Triangle Fan";
  59.     /* Set virtual functions */
  60.     this->delete = delete_TriangleFan;
  61.     this->Layout = TriangleFan__Layout;
  62.     this->Copy = TriangleFan__Copy;
  63.     return this;
  64. }
  65.  
  66. void delete_TriangleFan(TestPtr thisTest)
  67. {
  68.     TriangleFanPtr this = (TriangleFanPtr)thisTest;
  69.     delete_Polygonal(thisTest);
  70. }
  71.  
  72. TestPtr TriangleFan__Copy(TestPtr thisTest)
  73. {
  74.     TriangleFanPtr this = (TriangleFanPtr)thisTest;
  75.     TriangleFanPtr newTriangleFan = new_TriangleFan();
  76.     FreeStrings((TestPtr)newTriangleFan);
  77.     *newTriangleFan = *this;
  78.     CopyStrings((TestPtr)newTriangleFan, (TestPtr)this);
  79.     return (TestPtr)newTriangleFan;
  80. }
  81.  
  82. void TriangleFan__Layout(VertexPtr thisVertex)
  83. {
  84.     TriangleFanPtr this = (TriangleFanPtr)thisVertex;
  85.     const double pi=3.141592654;
  86.     double ndcSize, lengthSide, halfWidth, radius;
  87.     double theta, theta0, deltaTheta;
  88.     GLfloat x, y;
  89.     GLfloat *src, *dst;
  90.     GLfloat *newTraversalData;
  91.     int numFrontIn, numFrontOut, numFrontClip;
  92.     int numBackIn, numBackOut, numBackClip;
  93.     int frontIn, frontOut, frontClip, backIn, backOut, backClip;
  94.     GLfloat face=1.0;
  95.     int i, j;
  96.     GLfloat facingFront = this->facingFront;
  97.     GLfloat facingBack  = this->facingBack;
  98.     GLfloat numObjects  = this->numObjects;
  99.     GLfloat acceptObjs  = this->acceptObjs;
  100.     GLfloat rejectObjs  = this->rejectObjs;
  101.     GLfloat clipObjs     = this->clipObjs;
  102.     int   windowDim   = min(this->environ.windowWidth, this->environ.windowHeight);
  103.     int   orientation = this->orientation;
  104.     GLfloat size        = this->size;
  105.     int   objsPerBgnEnd  = this->objsPerBgnEnd;
  106.     int   layoutPoints = this->numBgnEnds;
  107.  
  108.     /* Use Vertex__Layout() and orientation to choose layout */
  109.     this->vertsPerBgnEnd = objsPerBgnEnd * this->vertsPerFacet + 2;
  110.     this->facetsPerBgnEnd = this->vertsPerBgnEnd/this->vertsPerFacet;
  111.  
  112.     /* Figure front facing/back facing stuff */
  113.     if (fabs(1.0 - facingFront - facingBack) > .01) {
  114.         printf("FrontFacing + BackFacing not equal to 1\n");
  115.         exit(1);
  116.     }
  117.     numFrontIn = floor((GLfloat)layoutPoints * acceptObjs * facingFront + 0.5);
  118.     numFrontOut = floor((GLfloat)layoutPoints * rejectObjs * facingFront + 0.5);
  119.     numFrontClip = floor((GLfloat)layoutPoints * clipObjs * facingFront + 0.5);
  120.     numBackIn = floor((GLfloat)layoutPoints * acceptObjs * facingBack + 0.5);
  121.     numBackOut = floor((GLfloat)layoutPoints * rejectObjs * facingBack + 0.5);
  122.     numBackClip = floor((GLfloat)layoutPoints * clipObjs * facingBack + 0.5);
  123.     numFrontIn += layoutPoints - numFrontIn - numFrontOut - numFrontClip
  124.                              - numBackIn - numBackOut - numBackClip;
  125.     frontIn = frontOut = frontClip = backIn = backOut = backClip = 0;
  126.  
  127.     /* Figure polygon dimensions given polygon size in pixels and number of sides */
  128.     ndcSize = size/(double)windowDim/(double)windowDim*4.0;
  129.     if (this->vertsPerBgnEnd<5) {
  130.     radius = sqrt(2.0*ndcSize);
  131.     deltaTheta = pi/2.0;
  132.     } else {
  133.     ndcSize *= (GLfloat)objsPerBgnEnd;
  134.     halfWidth = sqrt(ndcSize/(double)objsPerBgnEnd/tan(pi/(double)objsPerBgnEnd));
  135.     lengthSide = 2.0*ndcSize/halfWidth/(double)objsPerBgnEnd;
  136.     radius = sqrt(sq(lengthSide/2.0) + sq(halfWidth));
  137.     deltaTheta = 2.0*pi/(double)objsPerBgnEnd;
  138.     }
  139.  
  140.     this->layoutPoints = layoutPoints;
  141.     this->layoutPadding = radius;
  142.     this->layoutPadding += (1. + (this->antiAlias!=Off))/
  143.                            (float)windowDim;
  144.     Vertex__Layout(thisVertex);
  145.  
  146.     /* Allocate necessary data */
  147.     src = this->traversalData;
  148.     newTraversalData = (GLfloat*)malloc(this->numBgnEnds * this->vertsPerBgnEnd * 2 * sizeof(GLfloat));
  149.     CheckMalloc(newTraversalData);
  150.     dst = newTraversalData;
  151.  
  152.     /* Figure initial theta position for orientation */
  153.     switch (orientation) {
  154.         case Horizontal:
  155.         if (this->vertsPerBgnEnd<5) {
  156.             theta0 = 0.0;
  157.         } else {
  158.         theta0 = 3.0*pi/2.0 - deltaTheta/2.0;
  159.         }
  160.             break;
  161.         case Vertical:
  162.         if (this->vertsPerBgnEnd<5) {
  163.             theta0 = pi/2.0;
  164.         } else {
  165.         theta0 = pi - deltaTheta/2.0;
  166.         }
  167.         case Random:
  168.             mysrand(15000);
  169.             break;
  170.     }
  171.  
  172.     /* Generate those vertices around the point centers */
  173.     for (i=0; i<this->numBgnEnds; i++) {
  174.         x = *src++;
  175.         y = *src++;
  176.         if (orientation == Random)
  177.             theta0 = 2.0*pi*(double)myrand()/(double)MY_RAND_MAX;
  178.         if ((x==-1 || x==1) && (-1<=y && y<=1) || (y==-1 || y==1) && (-1<=x && x<=1)) {
  179.             /* The vertex is clipped (on the viewport boundary) */
  180.             if (frontClip < numFrontClip) {
  181.                 frontClip++;
  182.                 face = 1.0;
  183.             } else {
  184.                 backClip++;
  185.                 face = -1.0;
  186.             }
  187.         } else if (-1<=x && x<=1 && -1<=y && y<=1) {
  188.             /* The vertex is in the viewport */
  189.             if (frontIn < numFrontIn) {
  190.                 frontIn++;
  191.                 face = 1.0;
  192.             } else {
  193.                 backIn++;
  194.                 face = -1.0;
  195.             }
  196.         } else {
  197.             /* The vertex is outside the viewport */
  198.             if (frontOut < numFrontOut) {
  199.                 frontOut++;
  200.                 face = 1.0;
  201.             } else {
  202.                 backOut++;
  203.                 face = -1.0;
  204.             }
  205.         }
  206.         theta = face*theta0;
  207.     *dst++ = x;
  208.     *dst++ = y;
  209.         for (j=1; j<this->vertsPerBgnEnd; j++) {
  210.             *dst++ = radius * cos(theta) + x;
  211.             *dst++ = radius * sin(theta) + y;
  212.             theta += face*deltaTheta;
  213.         }
  214.     }
  215.     free(this->traversalData);
  216.     this->traversalData = newTraversalData;
  217. }
  218.